﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace StockAnalyzer.Core
{
    class Stock
    {
        public Stock(string symbol)
        {
            Symbol = symbol;
        }

        public readonly string Symbol;
        public readonly List<Tick> History = new List<Tick>();

        public override string ToString()
        {
            return Symbol;
        }
    }

    internal class StockExtend
    {
        public readonly Stock stock;
        public StockExtend(Stock stock)
        {
            this.stock = stock;
        }
    }

    class Tick : IComparable<Tick>
    {
        public readonly DateTime Date;
        public readonly double Open;
        public readonly double Close;
        public readonly double High;
        public readonly double Low;
        public readonly Int64 Volume;

        public Tick(DateTime date, double open, double close, double high, double low, Int64 volume)
        {
            Date = date;
            Open = open;
            Close = close;
            High = high;
            Low = low;
            Volume = volume;
        }

        int IComparable<Tick>.CompareTo(Tick other)
        {
            if (other.Close > this.Close)
                return -1;
            else if (other.Close == this.Close)
                return 0;
            else
                return 1;

        }

        public override string ToString()
        {
            return "date=" + Date + ", open=" + Open + ", close=" + Close + ", high=" + High + ", low=" + Low;
        }
    }

    internal class StockCollection: IEnumerable
    {
        private readonly List<Stock> list = new List<Stock>();

        public void Append(string symbol)
        {
            symbol = symbol.Trim();
            if (symbol.Length == 0)
            {
                return;
            }

            foreach (Stock s in list)
            {
                if (s.Symbol.ToUpper() == symbol.ToUpper())
                {
                    return;
                }
            }
            list.Add(new Stock(symbol));
        }

        public Stock this[string symbol]
        {
            get
            {
                foreach (var s in list)
                {
                    if (s.Symbol == symbol)
                    {
                        return s;
                    }
                }
                throw new Exception("不存在代号为[" + symbol + "]的股票");
            }
        }

        public void Sort(IComparer<Stock> comparer)
        {
            list.Sort(comparer);
        }

        #region IEnumerable Members

        public IEnumerator GetEnumerator()
        {
            int i = 0;
            while (i < list.Count)
            {
                yield return list[i++];
            }
        }

        #endregion
    }
}